package com.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.sys.entity.SysMessage;
import com.sys.service.SysMessageService;
import com.bhudy.base.QueryFilter;
import com.bhudy.enums.Relation;
import com.bhudy.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author bhudy
 * @Description SysMessageController
 * @Date Tue Nov 12 10:57:04 CST 2019
 */
@RestController
public class SysMessageController {

    @Autowired
    private SysMessageService sysMessageService;

    /**
     * 获取sysMessage的数据
     *
     * @param page  页数{1}
     * @param limit 显示数量{10}
     * @return sysMessage的list
     */
    @GetMapping(value = "/api/v1/sysMessages")
    public ResponseResult getAll(Integer page, Integer limit) {
        QueryFilter queryFilter = new QueryFilter().and("status", Relation.NOT_EQUAL, 1);
        if (page != null && limit != null) queryFilter.limit(--page, limit);
        return ResponseResult.printMapJson(0, sysMessageService.count(queryFilter), sysMessageService.findList(queryFilter));
    }

    @GetMapping(value = "/api/v1/sysMessage")
    public ResponseResult get(Long id) {
        return ResponseResult.printMapJson(0, sysMessageService.get(id));
    }

    @PostMapping(value = "/api/v1/sysMessage")
    public ResponseResult insert(@RequestBody String json) {
        return ResponseResult.printMapJson(0, sysMessageService.insert(JSONObject.parseObject(json, SysMessage.class)));
    }

    @PutMapping(value = "/api/v1/sysMessage")
    public ResponseResult update(@RequestBody String json) {
        return ResponseResult.printMapJson(0, sysMessageService.update(JSONObject.parseObject(json, SysMessage.class)));
    }

    @DeleteMapping(value = "/api/v1/sysMessages")
    public ResponseResult delete(@RequestBody String json) {
        return ResponseResult.printMapJson(0, sysMessageService.delete(JSONObject.parseObject(json, Long[].class)));
    }

}
